'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 查询对手列表(或黑名单列表)
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var battleModel = require('../../../../models/battleModel');
var rivalService = require('../../../../services/rivalService');
var roleService = require('../../../../services/roleService');
var heroConf = require('../../../../config/heroConf');
var PlayerManager = require('../../../../libs/playerManager');
var pvpModel = require('../../../../models/pvpModel.js');
var roleModel = require('../../../../models/roleModel.js');
var buildModel = require('../../../../models/buildModel.js');
var techModel = require('../../../../models/techModel.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var constantUtil = require('../../../../util/constantUtil');
var pvpService = require('../../../../services/pvpService.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var get_black_list = msg.get_black_list;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['pvp', 'accrued'], cb);
            },
            function (cb) {
                var pvp_model = player_manager.pvp;
                if (!!get_black_list) {
                    // 掠夺黑名单
                    _getRoleList(pvp_model.black_list, cb);
                } else {
                    // 掠夺对手
                    _getRoleList(pvp_model.player_list, cb);
                }
            }
        ],
        function (err, pvp_role_list) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            var result = {};
            if (!!get_black_list) {
                result.pvp_black_list = pvp_role_list;
            } else {
                result.pvp_role_list = pvp_role_list;
            }
            return next(null, {
                code: Code.OK,
                result: result
            });
        }
    );
};

var _getRoleList = function (player_list, callback) {
    var idx = 0;
    var role_id_list = [];
    var role_to_road = {};
    var role_to_fight_time = {};
    _.each(player_list, function (player_info) {
        role_id_list.push(player_info.role_id);
        if (!!player_info.road_id) {
            role_to_road[player_info.role_id] = player_info.road_id;
        } else {
            role_to_fight_time[player_info.role_id] = player_info.add_time;
        }
    });
    var where = {
        role_id: {
            $in: role_id_list
        }
    };

    var role_list;
    var build_list;
    var tech_list;
    var pvp_role_list = [];
    var build_map = {};
    var tech_map = {};
    async.waterfall(
        [
            function (cb) {
                roleModel.findRoles(where, cb);
            },
            function (list, cb) {
                role_list = list;
                buildModel.findBuilds(where, cb);
            },
            function (list, cb) {
                build_list = list;
                _.forEach(build_list, function (item) {
                    build_map[item.role_id] = item;
                });
                techModel.findTeches(where, cb);
            },
            function (list, cb) {
                tech_list = list;
                _.forEach(tech_list, function (item) {
                    tech_map[item.role_id] = item;
                });

                async.eachSeries(role_list, function (role_item, cb1) {
                    _getUnionModel(role_item.union_id, function (err, model) {
                        var union_model = model;
                        // var build_model = _.find(build_list, {
                        //     role_id: role_item.role_id
                        // });
                        // var tech_model = _.find(tech_list, {
                        //     role_id: role_item.role_id
                        // });
                        var build_model = build_map[role_item.role_id];
                        var tech_model = tech_map[role_item.role_id];

                        var harry_res = pvpService.getHarryRes(role_item, build_model, tech_model, union_model);
                        var gold = harry_res.gold;
                        var food = harry_res.food;
                        var pvp_role_item = {
                            role_id: role_item.role_id,
                            name: role_item.name,
                            job_type: role_item.job_type,
                            lv: role_item.lv,
                            gold: gold,
                            food: food,
                            vip: role_item.vip,
                            noble: role_item.noble,
                            main_power: role_item.main_power
                        };
                        var road_id = role_to_road[role_item.role_id];
                        if (!!road_id) {
                            pvp_role_item.road_id = road_id;
                        }
                        var fight_time = role_to_fight_time[role_item.role_id];
                        if (!!fight_time) {
                            pvp_role_item.fight_time = fight_time;
                        }
                        pvp_role_list.push(pvp_role_item);

                        cb1();
                    });
                }, function (err) {
                    if (!!err) {
                        return callback(err);
                    }
                    cb();
                });
           }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback(null, []);
            }
            callback(null, pvp_role_list);
        }
    );
};

var _getUnionModel = function (union_id, cb) {
    if (union_id === 0) {
        return cb(null, null);
    }

    unionGroupModel.getById(union_id, cb);
};
